﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using hcom;
using System.Data;
using System.Text;

namespace hcms.Admin
{
	public partial class ColumnEdit : System.Web.UI.Page
	{
		public string submitstate;
		//public string type;
		protected string cname;
		protected string cid, pid;
		private hString hstr = new hString();
		public DataRowCollection posrows;
		public Model model = Global.model;
		protected void Page_Load(object sender, EventArgs e)
		{
			cid = Request["cid"];	// 编辑cid栏目
			pid = Request["pid"];	// 在pid栏目下新建栏目

			posrows = model.getCurrentPos(pid);

			if (Page.IsPostBack)
			{
				switch (cmd.Value)
				{
					case "save":
						save();
						break;
					default:
						throw (new Exception("不可识别的命令: \"" + cmd.Value + "\""));
				}
			}
			else
			{
				// 初始化栏目模板下拉框
				ListItem li = new ListItem("", "0");
				idtemplateid.Items.Add(li);
				DataRowCollection rows = model.getTemplateRows("t");
				int n = rows.Count;
				for (int i = 0; i < n; i++)
				{
					DataRow row = rows[i];
					li = new ListItem(row["temName"].ToString(), row["temID"].ToString());
					idtemplateid.Items.Add(li);
				}

				// 初始化文档模板下拉框
				li = new ListItem("", "0");
				idarttemplate.Items.Add(li);
				rows = model.getTemplateRows("a");
				n = rows.Count;
				for (int i = 0; i < n; i++)
				{
					DataRow row = rows[i];
					li = new ListItem(row["temName"].ToString(), row["temID"].ToString());
					idarttemplate.Items.Add(li);
				}

				if (cid == null || cid.Trim().Length == 0)
				{
					cname = "新建";
				}
				else
				{
					DataRow row = model.getColumnByID(cid);
					idname.Value = ((string)row["colName"]).Trim();
					idtemplateid.SelectedValue = row["colTemplateID"].ToString();
					idarttemplate.SelectedValue = row["colArtTemplateID"].ToString();
					idlevel.Value = row["colLevel"].ToString();
					idtag.Value = row["colTag"].ToString();
					//idpagetitle.Value = row["colBroswerTitle"].ToString();
					iddescription.Value = row["colDescription"].ToString();

					cname = idname.Value;
				}
			}
		}

		private void save()
		{
			if (cid == null || cid.Trim().Length == 0)	// 创建新栏目
			{
				lock (model.da)	// 锁定线程，防止getnewtid线程冲突
				{
					cid = model.getnewtid(pid);
					string strsql = string.Format("insert into hcms_Columns( colID, colName, colDescription, " +
						"colTemplateID, colArtTemplateID, colTag, colLevel) " +
						"values('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}')",
						cid,
						hstr.ToSQL(idname.Value),
						hstr.ToSQL(iddescription.Value),
						idtemplateid.SelectedValue,
						idarttemplate.SelectedValue,
						hstr.ToSQL(idtag.Value),
						idlevel.Value/*,
						idpagetitle.Value*/
						);
					model.da.Execute(strsql);
				}
			}
			else
			{
				string strsql = string.Format("UPDATE hcms_Columns SET colName='{0}', colDescription='{1}', " +
					"colTemplateID={2}, colArtTemplateID={3}, colTag='{4}', colLevel='{5}' WHERE colID = '{6}'",
					hstr.ToSQL(idname.Value),
					hstr.ToSQL(iddescription.Value),
					idtemplateid.SelectedValue,
					idarttemplate.SelectedValue,
					hstr.ToSQL(idtag.Value),
					idlevel.Value,
					//idpagetitle.Value,
					cid
					);
				model.da.Execute(strsql);

				// 删除栏目对应的静态页，因为修改栏目时可能改变文档模板。
				model.delFiles("\\" + cid);
			}

			Response.Write("<script>window.parent.refreshTree();</script>"); //刷新目录树。
			submitstate = "保存完毕";
		}

	}
}